package core;

import gitobject.Commit;
import gitobject.GitObject;
import picocli.CommandLine.Command;
import repository.Repository;
import utility.Utility;

@Command(
        name = "log"
)
/**
 * 实现了jit log命令，命令行打印处jit日志
 */
public class JitLog implements Runnable {
    @Override
    public void run() {
        Repository repo = Repository.getInstance();
        if (!repo.exist()) {
            System.err.println("No Repo Found");
            System.exit(-1);
        }
        try {
            byte[] key = repo.getCurrentBranch().getCommit();
            if (key == null) {
                System.out.println("No Commit.");
                return;
            }
            while (key != null) {
                Commit commit = (Commit) GitObject.findByKey(key);
                System.out.printf("commit %s%n", Utility.hex2string(commit.getKey()));
                System.out.printf("Author: %s%n", commit.getAuthor());
                System.out.printf("Date: %s%n", commit.getTime());
                System.out.println();
                System.out.printf("\t%s%n", commit.getMessage());
                System.out.println();
                key = commit.getParent();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
